package com.example.jack.community.view.home;

import android.content.Intent;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jack.community.BaseActivity;
import com.example.jack.community.R;

public class HomeScanningActivity extends BaseActivity {
    private ImageView home_back;
    private WebView home_web;
    private ProgressBar home_progress;
    private TextView mTextView;
    private LinearLayout mLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_scanning);
        initView();
        Intent intent = this.getIntent();
        String valueUrl = intent.getStringExtra("key");
        boolean contains = valueUrl.contains("http://");//判断是否包含http
        Toast.makeText(this, valueUrl, Toast.LENGTH_SHORT).show();
        initListenter(valueUrl);


    }

    private void initListenter(final String valueUrl) {
        home_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        // 设置允许加载混合内容
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            home_web.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        //允许JavaScript效果
        home_web.getSettings().setJavaScriptEnabled(true);

            home_web.setWebViewClient(new WebViewClient() {
                //设置不用系统浏览器打开,直接显示在当前Webview
//                @Override
//                public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                    view.loadUrl(url);
//                    return true;
//                }
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                    view.loadUrl(request.toString());
                    return true;
                }
                // 允许所有SSL证书
                @Override
                public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                    handler.proceed();
                }

            });

            home_web.setWebChromeClient(new WebChromeClient() {
                @Override
                public void onProgressChanged(WebView view, int newProgress) {
                    super.onProgressChanged(view, newProgress);
                    //先隐藏线性布局
                    mLayout.setVisibility(View.INVISIBLE);
                    home_web.setVisibility(View.INVISIBLE);
                    if (newProgress < 100) {
                        String progress = newProgress + "%";
                        mTextView.setText(progress);

                    } else if (newProgress == 100) {//等到一百是显示webview隐藏进度条

                        String progress = newProgress + "%";
                        mTextView.setText(progress);
                        mTextView.setVisibility(View.GONE);
                        home_progress.setVisibility(View.GONE);
                        mLayout.setVisibility(View.VISIBLE);
                        home_web.setVisibility(View.VISIBLE);
                    }
                }
            });

        home_web.loadUrl(valueUrl);
    }




    private void initView() {
        mLayout = (LinearLayout) findViewById(R.id.home_scanning_web);
        home_back = (ImageView) findViewById(R.id.home_image_back);
       // home_web = (WebView) findViewById(R.id.home_scanning_web);
        home_progress = (ProgressBar) findViewById(R.id.home_progressBar);
        mTextView = (TextView) findViewById(R.id.home_scanning_text);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
                );
        home_web = new WebView(getApplicationContext());
        home_web.setLayoutParams(params);
        mLayout.addView(home_web);
    }
    //点击返回上一页面而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && home_web.canGoBack()) {
            home_web.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        //在销毁是移除web所有东西，防止OOM
      if (home_web != null){
          home_web.removeAllViews();
          home_web.loadDataWithBaseURL(null,"","text/html","utf-8",null);
          home_web.clearHistory();
          //获取到home_web的父View，然后将它移除，若不加这个可能会在小米手机5.1版本出现闪退的问题
          ((ViewGroup) home_web.getParent()).removeView(home_web);
          home_web.destroy();
          home_web = null;
      }
        super.onDestroy();

    }
}
